//
//  IntegralViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/8.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "IntegralViewController.h"

#import "IntegralTableViewCell.h"

#import "HeaderFileForEasyToTesco.h"
#import "AFNetworking.h"

#import "ReadLoginData.h"

@interface IntegralViewController ()<UITableViewDataSource, UITableViewDelegate>{

    NSString *usernameStr;
}

@property (nonatomic, strong)UITableView *Integraltableview;

@property (nonatomic, strong)NSMutableArray *integralArray;
@end

@implementation IntegralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.integralArray = [@[]mutableCopy];
    
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    }else {
        usernameStr = [dic[@"data"] objectForKey:@"username"];
        //获取积分数据
        [self getintegraldatasources];
    }
    
    
    
    
    [self addintegralTopviewUI];
    
    [self addintegraltableviewUI];
    
    
}

- (void)addintegralTopviewUI {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"积分详情";

}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addintegraltableviewUI {
    self.Integraltableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.9) style:UITableViewStyleGrouped];
    
    self.Integraltableview.rowHeight = 35;
    self.Integraltableview.dataSource = self;
    self.Integraltableview.delegate = self;
    self.Integraltableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.Integraltableview];
    
    
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.integralArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"IntegralTableViewCell";
        IntegralTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"IntegralTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            cell.timeLabel.layer.borderWidth = 1;
            cell.dayLabel.layer.borderWidth = 1;
            cell.wholeLabel.layer.borderWidth = 1;
            cell.contentView.backgroundColor = self.Integraltableview.tableHeaderView.backgroundColor; 
            
        }
    cell.timeLabel.text = [NSString stringWithFormat:@"%@", [self.integralArray[indexPath.row] objectForKey:@"addtime"]];
    
    cell.dayLabel.text = [NSString stringWithFormat:@"%@", [self.integralArray[indexPath.row] objectForKey:@"meiri"]];
    
    cell.wholeLabel.text = [NSString stringWithFormat:@"%@", [self.integralArray[indexPath.row] objectForKey:@"zongjifen"]];
        return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
<<<<<<< HEAD
    return 80;
=======
    return 50;
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerview = [[UIView alloc] init];
<<<<<<< HEAD
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, WEITH, 30)];
    label.text = [NSString stringWithFormat:@"当期总积分:%@", [[self.integralArray firstObject] objectForKey:@"zongjifen"]];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];
    [headerview addSubview:label];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, CGRectGetMaxY(label.frame)+10, 133*WEITHRATE, 30)];
=======
//
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, WEITH, 30)];
//    label.text = [NSString stringWithFormat:@"当期总积分:%@", [[self.integralArray firstObject] objectForKey:@"zongjifen"]];
//    label.textAlignment = NSTextAlignmentCenter;
//    label.font = [UIFont systemFontOfSize:16];
//    [headerview addSubview:label];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 10, 133*WEITHRATE, 30)];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    timeLabel.text = @"更新时间";
    timeLabel.font = [UIFont systemFontOfSize:14];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.layer.borderWidth = 1;
    [headerview addSubview:timeLabel];
    
<<<<<<< HEAD
    UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame), CGRectGetMaxY(label.frame)+10, 90*WEITHRATE, 30)];
=======
    UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame), 10, 90*WEITHRATE, 30)];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    dayLabel.text = @"当天增加积分";
    dayLabel.font = [UIFont systemFontOfSize:14];
    dayLabel.textAlignment = NSTextAlignmentCenter;
    dayLabel.layer.borderWidth = 1;
    [headerview addSubview:dayLabel];
    
<<<<<<< HEAD
    UILabel *wholeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(dayLabel.frame), CGRectGetMaxY(label.frame)+10, 82*WEITHRATE, 30)];
=======
    UILabel *wholeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(dayLabel.frame), 10, 82*WEITHRATE, 30)];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    wholeLabel.text = @"当前总积分";
    wholeLabel.font = [UIFont systemFontOfSize:14];
    wholeLabel.textAlignment = NSTextAlignmentCenter;
    wholeLabel.layer.borderWidth = 1;
    [headerview addSubview:wholeLabel];
    
    
    
    
    return headerview;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


//获取我的积分列表
- (void)getintegraldatasources {
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = usernameStr;
    
    
    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username]};
    
    __weak IntegralViewController *integSelf = self;
    
    [mgr POST:@"http://www.ylgw8.com/AppUser/Integ" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSDictionary *dic = [integSelf toArrayOrNSDictionary:responseObject];
 
        
        integSelf.integralArray = dic[@"data"];
    
#pragma mark----获取我的积分
        
        [integSelf.Integraltableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
         //         NSLog(@"数据请求错误原因:%@", error);
         //         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
